const Joi = require('joi');
const { objectId } = require('./custom.validator');

// 营养目标设置验证
const createNutritionGoal = {
  body: Joi.object().keys({
    userId: Joi.custom(objectId).required(),
    caloriesGoal: Joi.number().min(0).required(),
    nutritionGoals: Joi.object({
      protein: Joi.number().min(0),
      fat: Joi.number().min(0),
      carbohydrate: Joi.number().min(0),
      fiber: Joi.number().min(0),
      sugar: Joi.number().min(0),
      sodium: Joi.number().min(0),
    }).required(),
    macroRatio: Joi.object({
      protein: Joi.number().min(0).max(100),
      fat: Joi.number().min(0).max(100),
      carbohydrate: Joi.number().min(0).max(100),
    }).required(),
  }),
};

const updateNutritionGoal = {
  params: Joi.object().keys({
    userId: Joi.custom(objectId).required(),
  }),
  body: Joi.object().keys({
    caloriesGoal: Joi.number().min(0),
    nutritionGoals: Joi.object({
      protein: Joi.number().min(0),
      fat: Joi.number().min(0),
      carbohydrate: Joi.number().min(0),
      fiber: Joi.number().min(0),
      sugar: Joi.number().min(0),
      sodium: Joi.number().min(0),
    }),
    macroRatio: Joi.object({
      protein: Joi.number().min(0).max(100),
      fat: Joi.number().min(0).max(100),
      carbohydrate: Joi.number().min(0).max(100),
    }),
  }).min(1),
};

const getNutritionGoal = {
  params: Joi.object().keys({
    userId: Joi.custom(objectId).required(),
  }),
};

// 营养建议验证
const createNutritionSuggestion = {
  body: Joi.object().keys({
    userId: Joi.custom(objectId).required(),
    type: Joi.string().valid('营养均衡', '热量控制', '蛋白质补充', '碳水适当', '脂肪控制', '膳食纤维', '维生素补充', '钠摄入控制', '其他').required(),
    content: Joi.string().required(),
    relevantFoods: Joi.array().items(
      Joi.object({
        foodId: Joi.custom(objectId).required(),
        reason: Joi.string().required(),
      })
    ).min(1).required(),
  }),
};

const updateNutritionSuggestion = {
  params: Joi.object().keys({
    id: Joi.custom(objectId).required(),
  }),
  body: Joi.object().keys({
    type: Joi.string().valid('营养均衡', '热量控制', '蛋白质补充', '碳水适当', '脂肪控制', '膳食纤维', '维生素补充', '钠摄入控制', '其他'),
    content: Joi.string(),
    relevantFoods: Joi.array().items(
      Joi.object({
        foodId: Joi.custom(objectId).required(),
        reason: Joi.string().required(),
      })
    ).min(1),
    isRead: Joi.boolean(),
  }).min(1),
};

const getNutritionSuggestions = {
  query: Joi.object().keys({
    userId: Joi.custom(objectId),
    type: Joi.string().valid('营养均衡', '热量控制', '蛋白质补充', '碳水适当', '脂肪控制', '膳食纤维', '维生素补充', '钠摄入控制', '其他'),
    isRead: Joi.boolean(),
    startDate: Joi.date().iso(),
    endDate: Joi.date().iso().min(Joi.ref('startDate')),
    sortBy: Joi.string(),
    limit: Joi.number().integer().min(1),
    page: Joi.number().integer().min(1),
  }),
};

const getNutritionSuggestion = {
  params: Joi.object().keys({
    id: Joi.custom(objectId).required(),
  }),
};

const deleteNutritionSuggestion = {
  params: Joi.object().keys({
    id: Joi.custom(objectId).required(),
  }),
};

// 营养分析验证
const getNutritionAnalysis = {
  query: Joi.object().keys({
    userId: Joi.custom(objectId).required(),
    startDate: Joi.date().iso(),
    endDate: Joi.date().iso().min(Joi.ref('startDate')),
    period: Joi.string().valid('day', 'week', 'month').default('week'),
  }),
};

const getCaloriesTrend = {
  query: Joi.object().keys({
    period: Joi.string().valid('7', '30', '90').default('7'),
    userId: Joi.string().custom(objectId)
  })
};

const getNutrientRatio = {
  query: Joi.object().keys({
    period: Joi.string().valid('today', 'week', 'month').default('today'),
    userId: Joi.string().custom(objectId)
  })
};

const getNutrientDetails = {
  query: Joi.object().keys({
    period: Joi.string().valid('today', 'week', 'month').default('today'),
    userId: Joi.string().custom(objectId)
  })
};

// 为UI格式化的建议添加验证规则
const getNutritionSuggestionsUI = {
  query: Joi.object().keys({
    // 暂时允许任何格式的userId，调试期间不进行严格验证
    userId: Joi.string().required()
    // 取消objectId验证，查看是否还有错误
    // userId: Joi.string().custom(objectId)
  })
};

module.exports = {
  createNutritionGoal,
  updateNutritionGoal,
  getNutritionGoal,
  createNutritionSuggestion,
  updateNutritionSuggestion,
  getNutritionSuggestions,
  getNutritionSuggestion,
  deleteNutritionSuggestion,
  getNutritionAnalysis,
  getCaloriesTrend,
  getNutrientRatio,
  getNutrientDetails,
  getNutritionSuggestionsUI,
}; 